//
// Created by ashCheng on 2022/11/10.
//
#include <iostream>
#include <map>
#include <string>
#include <vector>

std::vector<std::string> split(const std::string& str,
                               const std::string& delim) {
  std::vector<std::string> res;
  if ("" == str) return res;
  //先将要切割的字符串从string类型转换为char*类型
  char* strs = new char[str.length() + 1];  //不要忘了
  strcpy(strs, str.c_str());

  char* d = new char[delim.length() + 1];
  strcpy(d, delim.c_str());

  char* p = strtok(strs, d);
  while (p) {
    std::string s = p;  //分割得到的字符串转换为string类型
    res.push_back(s);   //存入结果数组
    p = strtok(NULL, d);
  }

  return res;
}

int main() {
  //  首先 执行df -h找到挂载硬盘的大小信息并存到一个map里面
  FILE* fp;
  char content[1024];
  std::map<std::string, std::vector<std::string>> mapMnt;
  const std::string cmd = "df";
  fp = popen(cmd.c_str(), "r");
  if (fp == NULL) {
  } else {
    //去掉第一行，标题头
    fgets(content, sizeof(content) - 1, fp);
    while (fgets(content, sizeof(content) - 1, fp) != NULL) {
      std::string row(content);
      //去掉换行符
      row.erase(std::remove(row.begin(), row.end(), '\n'), row.end());
      std::vector<std::string> infos = split(row, " ");
      mapMnt[infos.at(8)] = infos;
    }
  }
  //对获取到的挂载硬盘和map进行匹配
  std::vector<std::string> paths{"/System/Volumes/Data"};
  for (const auto& i : paths) {
    std::map<std::string,std::vector<std::string>>::iterator singleInfo = mapMnt.find(i);
    if (singleInfo==mapMnt.end()) {
      std::cout << "1" << std::endl;
      break;
    } else {
      std::cout << singleInfo->first << std::endl;
      std::vector<std::string> infos = singleInfo->second;
      for (int i = 0; i < infos.size(); ++i) {
        std::cout << "*****" << infos[i] << "******** "<<std::endl;
      }
      std::cout << std::endl;
    }
  }
  pclose(fp);
}